package app.itgungnir.compose.wan.business.my_website

import android.content.res.Configuration
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.InsertLink
import androidx.compose.material.icons.filled.Title
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.text.HtmlCompat
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme
import app.itgungnir.compose.wan.widgets.*

@Composable
fun MyWebsiteItem(
    navController: NavController = rememberNavController(),
    item: MyWebsite?,
    editCallback: (MyWebsite) -> Unit,
    deleteCallback: (MyWebsite) -> Unit
) {
    item ?: return
    WanSwipeMenuCard(
        modifier = Modifier.padding(top = 10.dp),
        onClick = { /* TODO */ },
        menuItems = listOf(
            WanSwipeMenuItem(icon = Icons.Filled.Edit, title = "编辑", backgroundColor = MaterialTheme.colors.secondary) {
                editCallback.invoke(item)
            },
            WanSwipeMenuItem(icon = Icons.Filled.Delete, title = "删除", backgroundColor = MaterialTheme.colors.error) {
                deleteCallback.invoke(item)
            }
        )
    ) {
        Column(modifier = Modifier.fillMaxWidth()) {
            WanTitle(content = HtmlCompat.fromHtml("${item.name}", HtmlCompat.FROM_HTML_MODE_COMPACT).toString())
            WanInfo(content = item.link)
        }
    }
}

@Composable
fun ColumnScope.WebsiteEditDialogUI(
    origin: MyWebsite? = null,
    confirmCallback: (String, String) -> Unit,
    loadingState: State<Boolean> = mutableStateOf(false),
    errorState: State<Throwable?> = mutableStateOf(null)
) {
    val focusManager = LocalFocusManager.current
    val nameValue = remember { mutableStateOf(TextFieldValue(text = origin?.name ?: "")) }
    val linkValue = remember { mutableStateOf(TextFieldValue(text = origin?.link ?: "")) }
    val typeText = if (origin == null) "新增" else "修改"
    WanTitle(modifier = Modifier.align(Alignment.CenterHorizontally), content = "${typeText}网站信息")
    Spacer(modifier = Modifier.height(5.dp))
    WanOutlinedInput(state = nameValue, label = "名称", icon = Icons.Filled.Title, loadingState = loadingState)
    Spacer(modifier = Modifier.height(5.dp))
    WanOutlinedInput(state = linkValue, label = "链接", icon = Icons.Filled.InsertLink, loadingState = loadingState)
    Spacer(modifier = Modifier.height(15.dp))
    WanButton(modifier = Modifier.fillMaxWidth(), text = "确认$typeText", loadingState = loadingState) {
        focusManager.clearFocus()
        confirmCallback.invoke(nameValue.value.text, linkValue.value.text)
    }
    WanError(modifier = Modifier.padding(top = 5.dp), contentState = errorState)
}

@Preview(name = "MyWebsiteItem light mode", showBackground = false)
@Preview(name = "MyWebsiteItem dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = false)
@Composable
private fun MyWebsiteItemPreview() {
    WanComposePreviewTheme {
        val item = MyWebsite(
            id = 0L,
            name = "标题标题标题标题标题标题标题标题标题标题标题标题标题",
            link = "https://www.google.com/search/key=how-to-improve-jetpack-compose-skills"
        )
        MyWebsiteItem(item = item, editCallback = {}, deleteCallback = {})
    }
}

@Preview(name = "WebsiteEditDialog light mode", showBackground = false)
@Preview(name = "WebsiteEditDialog dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = false)
@Composable
private fun WebsiteEditDialogPreview() {
    val item = MyWebsite(
        id = 0L,
        name = "标题标题标题标题标题标题标题标题标题标题标题标题标题",
        link = "https://www.google.com/search/key=how-to-improve-jetpack-compose-skills"
    )
    WanDialogPreview {
        WebsiteEditDialogUI(origin = item, confirmCallback = { _, _ -> })
    }
}
